package com.zhongxing.administrator.spritelhd;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxing.administrator.spritelhd.adapter.SubViewAdater;
import com.zhongxing.administrator.spritelhd.utils.AnimUtil;
import com.zhongxing.administrator.spritelhd.view.ShopView;
import com.zhongxing.administrator.spritelhd.view.ShouyeView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private TextView tv_message;
    private ImageButton ib_btn_home;
    private  FrameLayout fl_fold;
    private LinearLayout ll_sub_btns;
    private  ImageButton ib_btn_shop;
    private ViewPager vp_sub_view;
    private RelativeLayout rl_root;
    private ProgressBar pb_fight;
    private ProgressBar pb_energy;
    private  ShopView mShopView;
    private ShouyeView mShouyeView;


    private List<View> mSubViewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
    }



    private void initViewpage(){
        mSubViewList = new ArrayList<>();
        mShopView = new ShopView(this);
        mShouyeView =new ShouyeView(this);
        mSubViewList.add(mShouyeView.getView());
        mSubViewList.add(mShopView.getView());
        SubViewAdater subViewAdater = new SubViewAdater(this,mSubViewList);
        vp_sub_view.setAdapter(subViewAdater);




    }


    private void initUI(){
        fl_fold = (FrameLayout) findViewById(R.id.fl_fold);
        tv_message = (TextView) findViewById(R.id.tv_message);
        ib_btn_home = (ImageButton) findViewById(R.id.ib_btn_home);
        ll_sub_btns = (LinearLayout) findViewById(R.id.ll_sub_btns);
        ib_btn_shop = (ImageButton) findViewById(R.id.ib_btn_shop);
        vp_sub_view =  (ViewPager) findViewById(R.id.vp_sub_view);
        rl_root = (RelativeLayout) findViewById(R.id.rl_root);
        pb_fight = (ProgressBar) findViewById(R.id.pb_fight);
        pb_energy = (ProgressBar) findViewById(R.id.pb_energy);






        AnimUtil.addOnTouchListener(tv_message,0,0,null);
        AnimUtil.addOnTouchListener(ib_btn_home,0,0,animListener);
        AnimUtil.addOnTouchListener(ib_btn_shop,0,0,animListener);
        AnimUtil.addOnTouchListener(fl_fold,0,0,animListener);
        vp_sub_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        initViewpage();

        updateTimer();







    }

    private void updateTimer(){
        new Thread(){
            @Override
            public void run() {
                while (true){
                    int progress =  pb_fight.getProgress();
                    if (progress == 100){
                        progress = 0;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pb_fight.setProgress(progress + 10);
                }

            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                while (true){
                    int progress =  pb_energy.getProgress();
                    if (progress == 100){
                        progress = 0;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pb_energy.setProgress(progress + 1);
                }

            }
        }.start();
    }


    AnimUtil.AnimListener animListener = new AnimUtil.AnimListener() {

        Boolean isDown =false;
        @Override
        public void down(View v) {

        }

        @Override
        public void move(View v) {

        }

        @Override
        public void up(View v) {
            switch (v.getId()) {
                case R.id.tv_message:
                    break;
                case R.id.ib_btn_home:
                    vp_sub_view.setCurrentItem(0);
                    rl_root.setBackgroundResource(R.mipmap.background);
                    mShopView.hide();
                    break;
                case R.id.ib_btn_shop:
                    vp_sub_view.setCurrentItem(1);
                    mShopView.show();
                    rl_root.setBackgroundResource(R.mipmap.bg_shop);
                    break;
                case R.id.fl_fold:
                    int subHeight = ll_sub_btns.getHeight() - 52;
                    if (isDown){
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 180f, 3600f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, 0f);
                    }else {
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, subHeight);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f, 180f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, subHeight);
                    }
                    isDown = !isDown;
                    break;
            }
        }
    };



}
